package org.example.server.goods.controller.category;

import lombok.extern.slf4j.Slf4j;
import org.example.api.goods.category.controller.CategoryControllerFeign;
import org.example.api.goods.category.request.CategoryCreateRequest;
import org.example.api.goods.category.request.CategorySearchWithLevelPageRequest;
import org.example.api.goods.category.request.CategorySearchWithLevelRequest;
import org.example.api.goods.category.request.CategoryUpdateRequest;
import org.example.api.goods.category.response.CategorySummaryResponse;
import org.example.frw.api.base.intf.ID;
import org.example.frw.api.base.intf.impl.IDPojo;
import org.example.server.goods.mapstruct.category.CategoryConverter;
import org.example.server.goods.service.category.CategoryService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class CategoryController implements CategoryControllerFeign {
  private final CategoryService categoryService;
  private final CategoryConverter categoryConverter;


  public CategoryController(CategoryService categoryService, CategoryConverter categoryConverter) {
    this.categoryService = categoryService;
    this.categoryConverter = categoryConverter;
  }

  @Override
  public ID createCategory(CategoryCreateRequest request) {
    return categoryService.createCategory(request);
  }

  @Override
  public List<CategorySummaryResponse> searchCategory(CategorySearchWithLevelPageRequest categorySearchRequest) {
    return categoryConverter.toCategorySummaryFromBo(categoryService.pageCategories(categorySearchRequest.getPage(),categorySearchRequest));
  }


  /**
   * 更新分类
   *
   * @param id            主键
   * @param updateRequest updateRequest
   */
  @PostMapping("/category-manage/categories/id/{id}")
  public void updateCategory(@PathVariable Long id, @RequestBody CategoryUpdateRequest updateRequest) {
    categoryService.updateCategory(new IDPojo(id), updateRequest);
  }

  @Override
  public Long searchCategoryCount(CategorySearchWithLevelRequest categorySearchRequest) {
    return categoryService.categoriesCount(categorySearchRequest);
  }

  @Override
  public CategorySummaryResponse findOne(Long id) {
    return categoryConverter.toCategorySummaryFromBo(categoryService.findOne(id));
  }

  @Override
  public void hide(Long id) {
    categoryService.hide(new IDPojo(id));
  }
}
